<?php

namespace app\model;

//use support\Model;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WpProductsCans
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wp_a_cans_by_product';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function list($page,$pageNum,$fields,$condition)
    {
        $offset = ($page - 1) * $pageNum;
        $model = Db::table('wp_a_cans_by_product')
            ->select($fields)
            ->where($condition);
        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);
        return json_encode(['list' => $list, 'count' => $count]);
    }


    public static function all($fields,$condition)
    {
        $list = Db::table('wp_a_cans_by_product')
            ->select($fields)
            ->where($condition)
            ->get();
        if($list){
            foreach ($list as $k)
            if ($k->series_type == " car" || $k->series_type == "car") {
                $k->car_type_no = 1;
            }elseif ($k->series_type == " SUV" || $k->series_type == "SUV"){
                $k->car_type_no = 2;
            }elseif ($k->series_type == " MPV" || $k->series_type == "MPV"){
                $k->car_type_no = 3;
            }elseif ($k->series_type == " 跑车" || $k->series_type == "跑车"){
                $k->car_type_no = 4;
            }elseif ($k->series_type == " 新能源" || $k->series_type == "新能源"){
                $k->car_type_no = 5;
            }else{
                $k->car_type_no = '';
            }
        }
        return json_encode($list);
    }

    public static function find($fields,$condition)
    {
        $list = Db::table('wp_a_cans_by_product')
            ->select($fields)
            ->where($condition)
            ->first();
        return json_encode($list);
    }
}